package ch.fhnw.qualiscan;

import ch.fhnw.qualiscan.application.layout.BaseLayout;
import ch.fhnw.qualiscan.core.db.pojos.Language;
import ch.fhnw.qualiscan.core.db.pojos.Role;
import ch.fhnw.qualiscan.core.helpers.UserSession;
import ch.fhnw.qualiscan.core.libraries.LanguageLibrary;
import ch.fhnw.qualiscan.core.libraries.LoaderLibrary;
import com.vaadin.ui.Window;
import eu.livotov.tpt.TPTApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Entry point of vaadin application. Creates the AppLoader instance.
 * @author Kevin Mettenberger, Benjamin Groehbiel
 *
 */

@SuppressWarnings("serial")
public class QualiscanApplication extends TPTApplication {
	
	Window mainWindow = new Window(Config.SITE_TITLE);
	
	Logger logger = LoggerFactory.getLogger("QualiscanApplication");

	private LoaderLibrary loaderLib;
    private LanguageLibrary languageLib;
    private BaseLayout baseLayout;
    private ApplicationData appData;
	
	@Override
	public void applicationInit() {
		appData = new ApplicationData();
		
		
		mainWindow = new Window();
		mainWindow.setSizeFull();
		mainWindow.setData(appData);

    	setMainWindow(mainWindow);
     	setTheme("qualiscan");
    	
        loadLibraries();
        setApplicationData();

        mainWindow.addComponent(baseLayout);
        
//		////////////////////
//		///-----DEBUG-----///
//
//        logger.info("DEBUG MODE IS ON!!!");
//		debug();
//
//		//////////////////////
	}
	
	private void loadLibraries() {
            loaderLib = new LoaderLibrary();
            loaderLib.initLibrary();

            languageLib = new LanguageLibrary();

            //TODO: set browser language.
            /*
            setLocale(((WebApplicationContext)getContext()).getBrowser().getLocale());
            String lang = getLocale().getLanguage();
            */
            languageLib.setLanguage("en");	
	}
	
	private void setApplicationData() {
		baseLayout = new BaseLayout();
		appData.setBaseLayout(baseLayout);
		
		appData.setLoaderLib(loaderLib);
		appData.setLanguageLib(languageLib);
		
	}
	
	private void debug() {
		
		Role role = new Role();
		role.setId(2);
		
		Language language = new Language();
		language.setId(2);
		language.setLanguage("English");
		language.setLocale("en");
		
		UserSession userSession = new UserSession();
		userSession.setId(14);
		userSession.setFirstname("Kevin");
		userSession.setLastname("Mettenberger");
		userSession.setEmail("kevin@mettenberger.ch");
		userSession.setRole(role);
		userSession.setLanguage(language);
		
		TPTApplication.getCurrentApplication().setUser(userSession);
	}

	@Override
	public void firstApplicationStartup() {}
	
}
